/*	Market Script
	Made by: Plankt
	Version: 1.0	*/

prontera,156,191,4	script	Market	124,{
//Start config
	set .@name$, "[InterWebz Market]";
	//where people can open their shops (merchant vending), format: mapname, x, y
	setarray .@market$, "prontera", 155, 186;
	set .@ac, 2000; //cost to add item for auction
	set .@maxitemallowed, 100; //how many items the user can have in their inventory when recieving their item
	//the item ids you cant put on auction
	setarray .@banned_itemid, 501, 502, 503, 504, 1201;
	//you have one option to change at the bottom, the chatroom name
//End config
mes .@name$;
mes "What do you want to do?";
next;
switch(select("Warp to vending area","How do I use the auction NPC?","Buy item","Check my item status","Add new item to auction")){
case 1:
	warp .@market$[0], .@market$[1], .@market$[2];
	break;
case 2:
	mes .@name$;
	mes "When users add new auctions, they appear on the auction page on 'www.valiantro.bounceme.net/wshop'. You find an item you want and write down the auction id.";
	next;
	mes .@name$;
	mes "Then you use the auction id with my 'Buy item' menu";
	next;
	mes .@name$;
	mes "You can view the items you have put on auction and when viewing them, decide whether you want to view next or remove.";
	break;
case 3:
	mes .@name$;
	mes "Auction ID?";
	next;
	input .@auction_id;
	if(query_sql ("SELECT `buyer_accid`, `item_id`, `item_name`, `item_price`, `item_ammount`, `seller_accid`, `item_refine`, `card0`, `card1`, `card2`, `card3` FROM `auction_npc` WHERE `auction_id` = '" + .@auction_id + "'", .@buyer_accid, .@item_id, .@item_name$, .@item_price, .@item_ammount, .@seller_accid, .@item_refine, .@card1, .@card2, .@card3, .@card4) == 0){
		mes .@name$;
		mes "There's no item with that auction ID";
		close;
	} else if(.@buyer_accid != 0){
		mes .@name$;
		mes "That item is sold";
		close;
	} else if(.@seller_accid == getcharid(3)){
		mes .@name$;
		mes "You can't buy your own items";
		close;
	} else{
		query_sql("SELECT `userid` FROM `login` WHERE `account_id` = '" + .@seller_accid + "'", .@seller_name$);
		mes .@name$;
		mes "Auction ID: ^FF0000" + .@auction_id + "^000000";
		mes "Seller: " + .@seller_name$;
		if(.@item_refine > 0){
			set .@item_name$, "+" + .@item_refine + " " + .@item_name$;
		}
		if(getitemslots(.@item_id) > 0){
			set .@item_name$, .@item_name$ + "[" + getitemslots(.@item_id) + "]";
		}
		mes "Item: [^FF0000" + .@item_id + "^000000] ^0000FF" + .@item_name$ + "^000000 ";
		mes "Ammount: ^FF0000" + .@item_ammount + "^000000";
		mes "Price: ^FF0000" + .@item_price + "^000000z";
		if(getitemslots(.@item_id) > 0){
			if(getitemname(.@card1) != "null"){
				mes "Cards:";
			} else {
				mes "No cards slotted";
			}
			for(set .@k, 1; getitemname(getd(".@card"+.@k)) != "null"; set .@k, .@k+1){
				mes getitemname(getd(".@card"+.@k));
			}
		}
		next;
		if(select("Buy","Cancel") == 2){
			mes .@name$;
			mes "Please come again";
			close;
		} else if(Zeny < .@item_price){
			mes .@name$;
			mes "You can't afford the item";
			close;
		}
		if(query_sql ("SELECT `buyer_accid`, `item_id`, `item_price`, `item_ammount`, `item_refine`, `card0`, `card1`, `card2`, `card3` FROM `auction_npc` WHERE `auction_id` = '" + .@auction_id + "'", .@buyer_accid, .@item_id, .@item_price, .@item_ammount, .@item_refine, .@card0, .@card1, .@card2, .@card3) == 0){
			mes .@name$;
			mes "The item got removed";
			close;
		} else if(.@buyer_accid != 0){
			mes .@name$;
			mes "The item got sold";
			close;
		} else {
			getinventorylist;
			if(@inventorylist_count > .@maxitemallowed){
				mes .@name$;
				mes "You have too many items in your inventory";
				close;
			}
			set Zeny, Zeny-.@item_price;
			getitem2 .@item_id,.@item_ammount,1,.@item_refine,0,.@card0,.@card1,.@card2,.@card3;
			query_sql "UPDATE `auction_npc` SET `buyer_accid` = '"+getcharid(3)+"' WHERE `auction_id` = '"+.@auction_id+"'";
			mes .@name$;
			mes "Item bought successfully";
		}
	}
	break;
case 4:
	if(query_sql ("SELECT `item_id` FROM `auction_npc` WHERE `seller_accid` = '" + getcharid(3) + "'", .@i) == 0){
		mes "You don't have any items for sale";
		close;
	}
	for(set .@i, 0; query_sql ("SELECT `auction_id`, `buyer_accid`, `item_id`, `item_name`, `item_price`, `item_ammount`, `item_refine`, `card0`, `card1`, `card2`, `card3` FROM `auction_npc` WHERE `seller_accid` = '" + getcharid(3) + "' LIMIT " + .@i + ", 1", .@auction_id, .@buyer_accid, .@item_id, .@item_name$, .@item_price, .@item_ammount, .@item_refine, .@card1, .@card2, .@card3, .@card4) != 0; set .@i, .@i + 1){
		set .@ii, 0;
			if(.@buyer_accid[.@ii] > 0){
				set .@status$, "Sold to char id: " + .@buyer_accid[.@ii];
				set .@sold$, "Recieve cash";
				set .@remove$, "";
			} else {
				set .@status$, "^FF0000Unsold^000000";
				set .@sold$, "";
				set .@remove$, "Remove item from auction";
			}
			mes "Auction ID: ^FF0000" + .@auction_id[.@ii] + "^000000";
			if(.@item_refine > 0){
				set .@item_name$, "+" + .@item_refine + " " + .@item_name$;
			}
			mes "Item: ^0000FF" + .@item_name$[.@ii] + "^000000 [^FF0000" + .@item_id[.@ii] + "^000000]";
			mes "Ammount: ^FF0000" + .@item_ammount + "^000000";
			mes "Price: ^FF0000" + .@item_price[.@ii] + "^000000z";
			mes .@status$;
			if(getitemslots(.@item_id[.@ii]) > 0){
				if(getitemname(.@card1) != "null"){
					mes "Cards:";
				} else {
					mes "No cards slotted";
				}
				for(set .@k, 1; getitemname(getd(".@card"+.@k)) != "null"; set .@k, .@k+1){
					mes getitemname(getd(".@card"+.@k));
				}
			}
			next;
		if(select("Next",.@sold$,.@remove$,"End") == 4){
			mes .@name$;
			mes "Please come again";
			close;
		} else if(@menu == 2){
			mes .@name$;
			if(Zeny > 999999999){
				mes "You have too much zeny at the moment, please come back when you have less then 1.000.000.000";
				close;
			}
			query_sql "DELETE FROM `auction_npc` WHERE `auction_id` = '"+.@auction_id[.@ii]+"'";
			set Zeny, Zeny+.@item_price[.@ii];
			mes "Money recieved";
			close;
		} else if(@menu == 3){
			mes .@name$;
			mes "The item will get removed from auction and you will get it back";
			mes "You will not get the auction fee back tho";
			next;
			if(query_sql ("SELECT `buyer_accid`, `item_id`, `item_name`, `item_price`, `item_ammount` FROM `auction_npc` WHERE `auction_id` = '" + .@auction_id[.@ii] + "'", .@buyer_accid, .@item_id, .@item_name$, .@item_price, .@item_ammount) == 0){
				mes .@name$;
				mes "There's no item with that auction ID";
				close;
			} else if(.@buyer_accid != 0){
				mes .@name$;
				mes "Item is sold";
			} else {
				mes .@name$;
				mes "Auction ID: ^FF0000" + .@auction_id + "^000000";
				if(.@item_refine > 0){
					set .@item_name$, "+" + .@item_refine + " " + .@item_name$;
				}
				mes "Item: ^0000FF" + .@item_name$ + "^000000 [^FF0000" + .@item_id + "^000000]";
				mes "Ammount: ^FF0000" + .@item_ammount + "^000000";
				mes "Price: ^FF0000" + .@item_price + "^000000z";
				if(getitemslots(.@item_id[.@ii]) > 0){
					if(getitemname(.@card1) != "null"){
						mes "Cards:";
					} else {
						mes "No cards slotted";
					}
					for(set .@k, 1; getitemname(getd(".@card"+.@k)) != "null"; set .@k, .@k+1){
						mes getitemname(getd(".@card"+.@k));
					}
				}
				next;
				if(select("Remove","Cancel") == 2){
					mes .@name$;
					mes "Please come again";
					close;
				}
				if(query_sql ("SELECT `buyer_accid`, `item_id`, `item_price`, `item_ammount`, `item_refine`, `card0`, `card1`, `card2`, `card3` FROM `auction_npc` WHERE `auction_id` = '" + .@auction_id + "'", .@buyer_accid, .@item_id, .@item_price, .@item_ammount, .@item_refine, .@card0, .@card1, .@card2, .@card3) == 0){
					mes .@name$;
					mes "The item got removed";
					close;
				} else if(.@buyer_accid != 0){
					mes .@name$;
					mes "The item got sold";
					close;
				} else {
					getinventorylist;
					if(@inventorylist_count > .@maxitemallowed){
						mes .@name$;
						mes "You have too many items in your inventory";
						close;
					}
					getitem2 .@item_id,.@item_ammount,1,.@item_refine,0,.@card0,.@card1,.@card2,.@card3;
					query_sql "DELETE FROM `auction_npc` WHERE `auction_id` = '"+.@auction_id+"'";
					mes .@name$;
					mes "Item removed from auction";
					close;
				}
			}	
		}
	}
	mes .@name$;
	mes "That was it.";
	break;
case 5:
	mes .@name$;
	mes "Which item do you want to add to the auction?";
	next;
	getinventorylist;
	for(set .@i, 0; .@i < @inventorylist_count; set .@i, .@i+1){
		if(@inventorylist_equip[.@i] || !@inventorylist_identify[.@i] || @inventorylist_attribute[.@i]){
			set .@menu$, .@menu$ + ":";
		} else {
			set .@additem$, getitemname(@inventorylist_id[.@i]);
			if(@inventorylist_refine[.@i] > 0){
				set .@additem$, .@additem$ + " +" + @inventorylist_refine[.@i];
			}
			if(getitemslots(@inventorylist_id[.@i]) > 0){
				set .@additem$, "[" + getitemslots(@inventorylist_id[.@i]) + "]" + .@additem$;
			}
			set .@menu$, .@menu$ + ":" + .@additem$;
		}
	}
	set .@menu$, .@menu$ + ":None";
	select(.@menu$);
	set .@menu, (@menu-2);
	if(@menu == (@inventorylist_count+2)){
		mes .@name$;
		mes "Welcome back";
		close;
	}
	for(set .@c, 0; .@c != getarraysize(.@banned_itemid); set .@c, .@c + 1){
		if(@inventorylist_id[.@menu] == .@banned_itemid[.@c]){
			mes .@name$;
			mes "You can't add that item to auction";
			close;
		}
	}
	if(.@ac > 0){
		mes .@name$;
		mes "There's a fee for putting up an auction.";
		mes "It's ^FF0000" + .@ac + "^000000z";
		mes "Do you still want to continue?";
		next;
		if(select("Yes","No") == 2){
			close;
		}
	}
	mes .@name$;
	mes "Which price do you want to sell it for?";
	mes "1 - 10.000.000";
	next;
	input .@aprice;
	if((.@aprice < 1) || (.@aprice > 9999999)){
		if(.@aprice == 10000000){
			mes .@name$;
			mes "The price will be ^FF000010.000.000^000000z";
			next;
		} else {
			mes .@name$;
			mes "Please enter a value between ^FF00001^000000 and ^FF000010.000.000^000000";
			close;
		}
	}
	if(@inventorylist_amount[.@menu] > 1){
		mes .@name$;
		mes "Which amount to sell?";
		mes "1 - " + @inventorylist_amount[.@menu];
		next;
		input .@aamount;
		if(.@aamount < 1 || .@aamount > @inventorylist_amount[.@menu]){
			mes "Please enter a value between ^FF00001^000000 and ^FF0000" + @inventorylist_amount[.@menu] + "^000000";
			close;
		}
	} else {
	next;
		set .@aamount, 1;
	}
	if(Zeny < .@ac){
		mes .@name$;
		mes "You can't afford the auction fee";
		close;
	}
	if(attachrid(getcharid(3)) != 1){
		close;
	}
	set Zeny, Zeny-.@ac;
	delitem2 @inventorylist_id[.@menu],.@aamount,@inventorylist_identify[.@menu],@inventorylist_refine[.@menu],@inventorylist_attribute[.@menu],@inventorylist_card1[.@menu],@inventorylist_card2[.@menu],@inventorylist_card3[.@menu],@inventorylist_card4[.@menu];
	query_sql "INSERT INTO `auction_npc` (`seller_accid`, `buyer_accid`, `item_id`, `item_name`, `item_ammount`, `item_price`, `item_type`, `item_refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) VALUES('" + getcharid(3) + "', '0', '" + @inventorylist_id[.@menu] + "', '" + getitemname(@inventorylist_id[.@menu]) + "', '" + .@aamount + "', '" + .@aprice + "', '" + getiteminfo(@inventorylist_id[.@menu],2) + "', '" + @inventorylist_refine[.@menu] + "', '" + @inventorylist_attribute[.@menu] + "', '" + @inventorylist_card1[.@menu] + "', '" + @inventorylist_card2[.@menu] + "', '" + @inventorylist_card3[.@menu] + "', '" + @inventorylist_card4[.@menu] + "' )";
	mes .@name$;
	mes "Your item has successfully been added.";
	break;
}
close;

OnInit:
callfunc "mysql auction";
end;
}

function	script	mysql auction	{
query_sql "CREATE TABLE IF NOT EXISTS `auction_npc` ( `auction_id` int(11) NOT NULL auto_increment, `seller_accid` int(11) NOT NULL default '0', `buyer_accid` int(11) NOT NULL default '0', `item_id` int(11) NOT NULL default '0', `item_name` varchar(41) NOT NULL default 'ERROR', `item_ammount` smallint(4) NOT NULL default '0', `item_price` int(15) NOT NULL default '0', `item_type` smallint(6) NOT NULL default '0', `item_refine` tinyint(3) NOT NULL default '0', `attribute` tinyint(4) NOT NULL default '0', `card0` smallint(11) NOT NULL default '0', `card1` smallint(11) NOT NULL default '0', `card2` smallint(11) NOT NULL default '0', `card3` smallint(11) NOT NULL default '0', PRIMARY KEY  (`auction_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1";
setcell "prontera",148,156,163,195,cell_novending,1; //no vending between those coords
set .@waitingroom$, "No vending on the streets"; //the chatroom over the npc, set to "" to remove
if(.@waitingroom$ != ""){
	waitingroom .@waitingroom$,0;
}
return;
}